package cate.game.role.show.leaderskin;


import cate.common.table.d.GDBlog;
import cate.common.table.show.row.AvartarRow;
import cate.common.table.show.row.HeadIconRow;
import cate.common.table.show.row.LeaderSkinRow;
import cate.game.attr.FightAttr;
import cate.game.role.show.ShowBagTab;
import cate.game.role.show.avatar.AvatarVO;
import cate.game.role.show.icon.HeadIconVO;
import cate.game.role.show.msg.LeaderSkinOnResp;
import cate.game.role.show.msg.ModifyAvatarResp;
import cate.game.role.show.msg.ModifyHeadIconResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;

import java.util.ArrayList;
import java.util.List;


@NoteClass("主角皮肤")
public class RoleLeaderSkin extends ShowBagTab<LeaderSkin> {

    @Override
    public byte getType() {
        return GDBlog.DecorateType.LEADER_SKIN;
    }

    @Override
    protected LeaderSkin createItem(int tid, long validTime) {
        return new LeaderSkin(tid,validTime);
    }

    @Override
    public void onAddItem(LeaderSkin item) {
        role.getBase().updatePower();
    }

    @Override
    public void onInvalid(List<Integer> expireTids) {
        for (Integer skinId : expireTids) {
            // 脱掉主角穿着的皮肤
            if (role.getBase().leaderSkin == skinId) {
                role.getBase().leaderSkin = 0;
                role.sendNow(new LeaderSkinOnResp(0));
            }
            // 判断是否需要重置头像
            HeadIconVO headIconVO = role.getBase().headIcon;
            HeadIconRow headIconRow = role.getGame().table.blog.headIcon.get(headIconVO.tid);
            if (headIconRow != null && headIconRow.activeType == GDBlog.ActiveType.LEADER_SKIN && headIconRow.value == skinId) {
                headIconVO.tid = role.getShow().headIcon.findDefaultTid();
                role.sendNow(new ModifyHeadIconResp(role));

            }
            // 判断是否需要重置冒险形象
            AvatarVO avatarVO = role.getBase().avatar;
            AvartarRow avatarRow = role.getGame().table.blog.avartar.get(avatarVO.tid);
            if (avatarRow != null && avatarRow.activeType == GDBlog.ActiveType.LEADER_SKIN && avatarRow.value == skinId) {
                avatarVO.tid = role.getShow().avartar.findDefaultTid();
                role.sendNow(new ModifyAvatarResp(role));
            }
        }
    }

    @JsonIgnore
    public FightAttr getAttr() {
        FightAttr totalAttr = new FightAttr();
        List<LeaderSkin> list = new ArrayList<>(this.list);
        for (LeaderSkin skin : list) {
            LeaderSkinRow row = skin.getRow(role.getGame());
            if (row != null) {
                totalAttr.add(new FightAttr(row.attrStr));
            }
        }
        return totalAttr;
    }
}
